//
//  ForgetPasswordView.swift
//  XGamesLive
//
//  Created by ios on 2024/5/14.
//

import UIKit
import MXCommonModule

protocol ForgetPasswordDelegate: NSObjectProtocol {
    func getCode()
    func createAccount()
    func nextStep()
}

class ForgetPasswordView: UIView {
    weak var delegate: ForgetPasswordDelegate?
    private var emailText: UITextField!
    private var code1Text: CustomUITextField!
    private var code2Text: CustomUITextField!
    private var code3Text: CustomUITextField!
    private var code4Text: CustomUITextField!
    
    var email: String{
        guard let text = self.emailText.text else { return "" }
        return text
    }
    var code: String{
        guard let code1 = self.code1Text.text else { return "" }
        guard let code2 = self.code2Text.text else { return "" }
        guard let code3 = self.code3Text.text else { return "" }
        guard let code4 = self.code4Text.text else { return "" }
        return "\(code1)\(code2)\(code3)\(code4)"
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.createXglView()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension ForgetPasswordView{
    @objc private func createAccountAction(){
        self.delegate?.createAccount()
    }
    @objc private func nextAction(){
        self.delegate?.nextStep()
    }
}

extension ForgetPasswordView: CustomUITextFieldDeleteDelegate{
    func customTextFieldDeleteBackward(textField: CustomUITextField) {
       if textField == code4Text{
            if textField.text!.count == 0 {
                self.code3Text.text = ""
                self.code3Text.becomeFirstResponder()
            }
        }else if textField == code3Text{
            if textField.text!.count == 0 {
                self.code2Text.text = ""
                self.code2Text.becomeFirstResponder()
            }
        }else if textField == code2Text{
            if textField.text!.count == 0 {
                self.code1Text.text = ""
                self.code1Text.becomeFirstResponder()
            }
        }
    }
}

extension ForgetPasswordView: UITextFieldDelegate{
    func textFieldDidChangeSelection(_ textField: UITextField) {
        if textField == self.code1Text {
            if textField.text!.count == 1 {
                self.code2Text.becomeFirstResponder()
            }
        }else if textField == code2Text{
            if textField.text!.count == 1 {
                self.code3Text.becomeFirstResponder()
            }
        }else if textField == code3Text{
            if textField.text!.count == 1 {
                self.code4Text.becomeFirstResponder()
            }
        }else if textField == code4Text{
            if textField.text!.count == 1 {
                self.code4Text.resignFirstResponder()
            }
        }
    }
    
    func textFieldDidEndEditing(_ textField: UITextField) {
        if textField == self.emailText{
            self.delegate?.getCode()
        }
    }
}



extension ForgetPasswordView{
    private func createXglView(){
        let itemWidth = (ScreenWidth - 15.0 * 5) / 4.0
        let iconView = LoginUntil.creatLoginIconView()
        self.emailText = UITextField().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 16.0)
            make.attributedPlaceholder = "Enter here".attributesColor(UIColor.hexColor("#A2A2A2"),UIFont.pingfangSC_Regular(fontSize: 16.0))
            make.delegate = self
        })
        let emailView = LoginUntil.creatLoginInputView(title: "Email", inputText: self.emailText)
        let codeTitleText = UILabel().then { make in
            make.text = "Enter Confirmation Code"
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
        }
        self.code1Text = CustomUITextField().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 30.0)
            make.layerCornerRadius = 12.0
            make.backgroundColor = UIColor.hexColor("#001310")
            make.textAlignment = .center
            make.delegate = self
            make.deleteDelegate = self
            make.keyboardType = .numberPad
        })
        self.code2Text = CustomUITextField().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 30.0)
            make.layerCornerRadius = 12.0
            make.backgroundColor = UIColor.hexColor("#001310")
            make.textAlignment = .center
            make.delegate = self
            make.deleteDelegate = self
            make.keyboardType = .numberPad
        })
        self.code3Text = CustomUITextField().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 30.0)
            make.layerCornerRadius = 12.0
            make.backgroundColor = UIColor.hexColor("#001310")
            make.textAlignment = .center
            make.delegate = self
            make.deleteDelegate = self
            make.keyboardType = .numberPad
        })
        self.code4Text = CustomUITextField().then({ make in
            make.textColor = UIColor.white
            make.font = UIFont.pingfangSC_Regular(fontSize: 30.0)
            make.layerCornerRadius = 12.0
            make.backgroundColor = UIColor.hexColor("#001310")
            make.textAlignment = .center
            make.delegate = self
            make.deleteDelegate = self
            make.keyboardType = .numberPad
        })
        let createView = UIView()
        let createHeadText = UILabel().then { make in
            make.text = "Don’t have account?"
            make.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
            make.textColor = .white
        }
        let createButton = UIButton().then { make in
            make.setTitle("Create one", for: .normal)
            make.setTitleColor(UIColor.hexColor("#80FFE6"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 14.0)
            make.addTarget(self, action: #selector(self.createAccountAction), for: .touchUpInside)
        }
        let nextButton = UIButton().then { make in
            make.setTitle("Next", for: .normal)
            make.setTitleColor(UIColor.hexColor("#191A1D"), for: .normal)
            make.titleLabel?.font = UIFont.pingfangSC_Regular(fontSize: 18.0)
            make.backgroundColor = UIColor.hexColor("#80FFE6")
            make.layerCornerRadius = 24.0
            make.addTarget(self, action: #selector(self.nextAction), for: .touchUpInside)
        }
        createView.addSubviews([createHeadText,createButton])
        self.addSubviews([iconView,emailView,codeTitleText,code1Text,code2Text,code3Text,code4Text,createView,nextButton])
        
        iconView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(conversionWithScrennWidth(value: 147.0))
            make.width.height.equalTo(conversionWithScrennWidth(value: 100.0))
        }
        emailView.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.top.equalTo(iconView.snp.bottom).offset(conversionWithScrennWidth(value: 60.0))
            make.centerX.equalToSuperview()
        }
        codeTitleText.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.top.equalTo(emailView.snp.bottom).offset(conversionWithScrennWidth(value: 30.0))
        }
        code1Text.snp.makeConstraints { make in
            make.left.equalTo(15.0)
            make.top.equalTo(codeTitleText.snp.bottom).offset(15.0)
            make.height.width.equalTo(itemWidth)
        }
        
        code2Text.snp.makeConstraints { make in
            make.left.equalTo(code1Text.snp.right).offset(15.0)
            make.centerY.width.height.equalTo(code1Text)
        }
        code3Text.snp.makeConstraints { make in
            make.left.equalTo(code2Text.snp.right).offset(15.0)
            make.centerY.width.height.equalTo(code1Text)
        }
        code4Text.snp.makeConstraints { make in
            make.left.equalTo(code3Text.snp.right).offset(15.0)
            make.centerY.width.height.equalTo(code1Text)
        }
        createHeadText.snp.makeConstraints { make in
            make.left.centerY.equalToSuperview()
        }
        createButton.snp.makeConstraints { make in
            make.left.equalTo(createHeadText.snp.right)
            make.centerY.height.equalToSuperview()
        }
        createView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.height.equalTo(20.0)
            make.right.equalTo(createButton)
            make.bottom.equalTo(nextButton.snp.top).offset(-30.0)
        }
        nextButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(conversionWithScrennWidth(value: 345.0))
            make.height.equalTo(48.0)
            make.bottom.equalTo(-BottomHeight-48.0)
        }
    }
}
